package com.czxy.tanhua.dubbo.api;

import com.czxy.tanhua.dubbo.mappers.UserInfoMapper;
import com.czxy.tanhua.dubbo.mongoapi.RecommendUserApi;
import com.czxy.tanhua.entity.UserInfo;
import com.czxy.tanhua.mongo.RecommendUser;
import com.czxy.tanhua.vo.RecommendPageVo;
import com.czxy.tanhua.vo.ResultList;
import com.czxy.tanhua.vo.UserInfoVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author 爱吃豆的土豆、
 * @Date 2023/4/7 16:10
 */
@DubboService
public class RecommendUserApiImpl implements RecommendUserApi {
    @Resource
    private UserInfoMapper userInfoMapper;
    @Resource
    private MongoTemplate mongoTemplate;
    @Override
    public ResultList findAllrecommendation(RecommendPageVo recommendPageVo, Long userId) {
        Criteria criteria = Criteria.where("toUserId").is(userId);
        //查询总数
        Query query = new Query(criteria);
        long count = mongoTemplate.count(query, RecommendUser.class);

        //查询当前页的数据列表
        Query querylimit = new Query(criteria).limit(recommendPageVo.getPagesize()).skip((recommendPageVo.getPage() - 1)*recommendPageVo.getPagesize()).with(Sort.by(Sort.Order.desc("score")));

        List<RecommendUser> recommendUserList = mongoTemplate.find(querylimit, RecommendUser.class);

        ResultList resultList = new ResultList<>();

        resultList.setItems(recommendUserList);
        //设置列表
        //设置当前页码
        resultList.setPage(recommendPageVo.getPage());
        //设置总记录数
        resultList.setCounts((int) count);
        //设置页大小
        resultList.setPagesize(recommendPageVo.getPagesize());
        //设置总页数
        resultList.setPages((int) (count/recommendPageVo.getPagesize()));

        return resultList;
    }

    @Override
    public UserInfoVo findtodayBest(Long userId) {
        Query query = new Query();
        //创建等值判断
        Criteria criteria = Criteria.where("toUserId").is(userId);
        Sort by = Sort.by(Sort.Order.desc("score"));
        query.addCriteria(criteria);
        query.with(by);

        RecommendUser one = mongoTemplate.findOne(query, RecommendUser.class);

        //查询数据
        UserInfo userInfo = userInfoMapper.selectById(one.getUserId());
        UserInfoVo userInfoVo = new UserInfoVo();
        userInfoVo.setId(userInfo.getId());
        userInfoVo.setTags(userInfo.getTags().split(","));
        userInfoVo.setAge(userInfo.getAge());
        userInfoVo.setNickname(userInfo.getNickname());
        userInfoVo.setGender(userInfo.getGender());
        userInfoVo.setAvatar(userInfo.getAvatar());
        userInfoVo.setFateValue(one.getScore().intValue());

        return userInfoVo;
    }

    @Override
    public UserInfoVo findUserInfo(Long id, Long userId) {
        //构建条件查询该用户的缘分值
        Query query = new Query();
        Criteria userId1 = Criteria.where("userId").is(id);
        Criteria toUserId = Criteria.where("toUserId").is(userId);
        query.addCriteria(userId1);
        query.addCriteria(toUserId);
        RecommendUser one = mongoTemplate.findOne(query, RecommendUser.class);
        //在mysql数据库中查询该用户的详情信息，处理，封装信息，返回用户信息
        UserInfo userInfo = userInfoMapper.selectById(id);
        UserInfoVo userInfoVo = new UserInfoVo();
        userInfoVo.setId(userInfo.getId());
        userInfoVo.setTags(userInfo.getTags().split(","));
        userInfoVo.setAge(userInfo.getAge());
        userInfoVo.setNickname(userInfo.getNickname());
        userInfoVo.setGender(userInfo.getGender());
        userInfoVo.setAvatar(userInfo.getAvatar());
        userInfoVo.setFateValue(one.getScore().intValue());
        return userInfoVo;
    }
}
